package giectech.spring.data.controller;

import giectech.spring.data.model.Coffee;
import giectech.spring.data.service.CoffeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Administrator
 * @title: RestUserController
 * @projectName springnewbucks
 * @description: TODO
 * @date 2022/9/717:16
 */
@RequestMapping("/restful")
//@RestController
@Controller
public class RestCoffeeController {

    @Autowired
    private CoffeeService service;

    @RequestMapping(value = "/queryCoffeeByName", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<List<Coffee>> queryCoffeeByName(@RequestParam("coffeeName") String coffeeName) {
        try {
            List<Coffee> coffeeList = service.findOneCoffeeByName(coffeeName);
            if (null == coffeeList) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(coffeeList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    @RequestMapping(value = "/queryCoffeeByName2", produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public ResponseEntity<List<Coffee>> queryCoffeeByName2(@RequestParam("coffeeName") String coffeeName) {
        try {
            List<Coffee> coffeeList = service.findOneCoffeeByName(coffeeName);
            if (null == coffeeList) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(coffeeList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
